#include "rtconfig.h"
#include "rtthread.h"
#include <rtdevice.h>
#include "finsh.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

static struct rt_alarm *alarm_time = RT_NULL;

static void alarmStart(int argc, char **args)
{
    if(alarm_time != RT_NULL)
        rt_alarm_start(alarm_time);
}

static void alarmStop(int argc, char **args)
{
    if(alarm_time != RT_NULL)
        rt_alarm_stop(alarm_time);
}

static void alarm_time_cb(rt_alarm_t alarm, time_t timestamp)
{
    rt_kprintf("alarm_time_cb is OK!\r\n");
}

static void alarmCreate(int argc, char **args)
{
    time_t now;
    struct tm tm_now;
    struct rt_alarm_setup alarm_setup;

    if(alarm_time != RT_NULL)
        return;
    
    now = time(NULL) + 60;
    gmtime_r(&now, &tm_now);

    alarm_setup.flag = RT_ALARM_DAILY;
    memcpy(&alarm_setup.wktime, &tm_now, sizeof(struct tm));

    alarm_time = rt_alarm_create(alarm_time_cb, &alarm_setup);
}

static void alarmDelete(int argc, char **args)
{
    if(alarm_time != RT_NULL)
    {
        if(rt_alarm_delete(alarm_time) == RT_EOK)
            alarm_time = RT_NULL;
    }
}


MSH_CMD_EXPORT_ALIAS(alarmStart, alarmStart, Start alarm);
MSH_CMD_EXPORT_ALIAS(alarmStop, alarmStop, Stop alarm);
MSH_CMD_EXPORT_ALIAS(alarmCreate, alarmCreate, Create alarm);
MSH_CMD_EXPORT_ALIAS(alarmDelete, alarmDelete, Delete alarm);
